package com.sayimo.maker.base;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.sayimo.framework.exception.ServiceException;
import com.sayimo.maker.base.service.AdpositionInterfaceService;
import com.sayimo.maker.base.service.AppManagerService;
import com.sayimo.maker.base.service.EmsService;
import com.sayimo.maker.base.service.FacebackService;
import com.sayimo.maker.base.service.UploadFileService;
import com.sayimo.maker.base.vo.Faceback;
import com.sayimo.maker.commons.Constant;


/**
 * @package com.sayimo.maker.web.base
 * @author gongyijun
 * @Title:BaseController
 * @Description:
 * @date 2017年1月23日
 */
@Controller
@RequestMapping("/base")
public class BaseController {

	private static Log logger = LogFactory.getLog(BaseController.class);
	@Resource(name = "uploadFileService")
	private UploadFileService uploadFileService;
	@Resource(name = "adpositionInterfaceServiceImpl")
	private AdpositionInterfaceService adpositionInterfaceServiceImpl;
	@Resource(name = "appManagerService")
	private AppManagerService appManagerService;
	@Resource(name = "emsService")
	private EmsService emsService;
	@Resource(name="facebackService")
	private FacebackService facebackService;
	
	
	
	/**
	 * @Package com.sayimo.maker.web.base
	 * @Author 龚益君
	 * @MethodName:uploadFilebackUrl
	 * @Description:上传图片返回URL
	 * @param request
	 * @return
	 * @Date 2017年1月11日,下午5:18:28
	 */
	@RequestMapping(value = "/uploadfilebackurl", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> uploadFilebackUrl(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = uploadFileService.writeFilePHP(request);
		} catch (ServiceException e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;
	}

	/**
	 * @Package com.sayimo.maker.web.base
	 * @Author 龚益君
	 * @MethodName:uploadFilebackUrl
	 * @Description:上传图片返回Id
	 * @param request
	 * @return
	 * @Date 2017年1月11日,下午5:18:28
	 */
	@RequestMapping(value = "/uploadfilebackid", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> uploadFilebackId(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = uploadFileService.writeFile(request);
		} catch (ServiceException e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;
	}
	
	
	/**
	 * 
	 * @Title: getAdpositionListByTag
	 * @Author: 田超
	 * @Date:日期：2017年1月9日 : 下午4:15:57
	 * @Description: 根据标示符获取广告
	 * @return Map<String,Object>
	 * @Version 1.0.0
	 */
	@RequestMapping(value = "/getadpositionlistbytag/{tag}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getAdpositionListByTag(@PathVariable("tag") String tag) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = adpositionInterfaceServiceImpl.getAdpositionListByTag(tag);
		} catch (ServiceException e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * @Title: getNewApp
	 * @Author: 田超
	 * @Date:日期：2017年1月10日 : 下午4:32:28
	 * @Description: 获取APP
	 * @return Map<String,Object>
	 * @Version 1.0.0
	 */
	@RequestMapping(value = "/getnewapp/{mobileSysType}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getNewApp(@PathVariable("mobileSysType") Integer mobileSysType) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = appManagerService.getNewApp(mobileSysType);
		} catch (ServiceException e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;
	}

	/**
	 * @Package com.sayimo.rainbow.web.base
	 * @Author 龚益君
	 * @MethodName:uploadFile
	 * @Description:上传文件
	 * @param files
	 * @param fileNames
	 * @param filePath
	 * @param imagePath
	 * @return
	 * @throws IOException
	 * @throws IllegalStateException
	 * @Date 2017年1月19日,下午6:05:09
	 */
	@RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile[] file,
			@RequestParam(value = "filePath", required = false) String filePath,
			@RequestParam(value = "imagePath", required = false) String imagePath) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		File[] fileCpy = new File[file.length];
		String[] fileNames = new String[file.length];
		try {
			for (int i = 0; i < file.length; i++) {
				File localFile = new File(file[i].getOriginalFilename());
				CommonsMultipartFile cf = (CommonsMultipartFile) file[i];
				DiskFileItem fi = (DiskFileItem) cf.getFileItem();
				localFile = fi.getStoreLocation();
				fileCpy[i] = localFile;
				fileNames[i] = file[i].getOriginalFilename();
			}
			resultMap = uploadFileService.uploadFile(fileCpy, fileNames, filePath, imagePath);
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;
	}
	
	/**
	 * @Package com.sayimo.rainbow.web.base
	 * @Author TANXIN
	 * @MethodName:getAllEms
	 * @Description:获取所有快递公司信息
	 * @return
	 * @Date 2017年1月16日,下午2:53:53
	 */
	@RequestMapping(value = "/getallems", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getAllEms() {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = emsService.getAllEms();
		} catch (ServiceException e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;
	}
	
	/**
	 * @Author chenpy
	 * @MethodName:insertFaceback
	 * @Description:
	 * @param request
	 * @return
	 * @Date 2017年1月23日,下午4:51:44
	 */
	@RequestMapping(value = "/insertfaceback", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> insertFaceback(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			Faceback faceback = new Faceback();
			faceback.setCustomerId((Integer) request.getAttribute("customerId"));
			faceback.setContentType(Integer.valueOf(request.getParameter("contentType")));
			faceback.setContent(request.getParameter("content"));
			faceback.setPhotoIds(request.getParameter("photoIds"));
			faceback.setLinkInfo(request.getParameter("linkInfo"));
			resultMap = facebackService.insertFaceback(faceback);
		} catch (ServiceException e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;
	}
	
}
